package com.aaa.car.controller;

import com.aaa.car.entity.MaintainRecord;
import com.aaa.car.service.MaintainRecordService;
import com.aaa.common.constant.ExceptionConstant;
import com.aaa.common.constant.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 维修记录表(MaintainRecord)表控制层
 *
 * @author makejava
 * @since 2020-11-23 15:58:54
 */
@RestController
@RequestMapping("maintainRecord")
public class MaintainRecordController {
    /**
     * 服务对象
     */
    @Resource
    private MaintainRecordService maintainRecordService;

    /**
     * 查询维修记录
     * @param pageNo
     * @param pageSize
     * @param brandname
     * @param maintainshopname
     * @return
     */
    @GetMapping("queryMaintainRecord")
    public Result queryMaintainRecord(Integer pageNo, Integer pageSize, String brandname, String maintainshopname) {
        if(pageNo==null||pageSize==null){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        return maintainRecordService.queryMaintainRecord(pageNo,pageSize,brandname,maintainshopname);
    }

    /**
     * 添加
     * @param maintainRecord
     * @return
     */
    @PostMapping("insert")
    public Result inset(@RequestBody  MaintainRecord maintainRecord){
        //判断新增的角色名字为空抛出异常
        if (maintainRecord.getMiantainShopShopid()==null){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }else{
            //返回成功
            return new Result(ReturnCode.SUCCESSS.getCode(),
                    ReturnCode.SUCCESSS.getMsg(), this.maintainRecordService.insert(maintainRecord));
        }
    }

    /**
     * 修改
     * @param maintainRecord
     * @return
     */
    @PutMapping("update")
    public Result update(@RequestBody MaintainRecord maintainRecord){
        //当ID空为时抛出自定义异常
        if (maintainRecord.getId()==null) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(), this.maintainRecordService.update(maintainRecord));
    }

    /**
     * 删除
     * @param Id
     * @return
     */
    @DeleteMapping("deleteById")
    public Result deleteById(Integer Id){
        //当ID空为时抛出自定义异常
        if (null == Id) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(), this.maintainRecordService.deleteById(Id));
    }
}